﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using ClassLibrary.DAO;
using ClassLibrary.Entity;
using ClassLibrary.Data;
using System.Collections.Generic;

public partial class Client_UserControl_ucShippingTo : System.Web.UI.UserControl
{
    TblShippToDAO shiptoDAO = new TblShippToDAO(Global.CONNECTION_STRING);
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadShippingTo();
        }
    }

    private void LoadShippingTo()
    {
        try
        {
            TblUserAccount account = new TblUserAccount();
            if (Session[Global.SESSION_LOGGED_USER] != null)
                account = (TblUserAccount)Session[Global.SESSION_LOGGED_USER];
            
            TblShippTo shipto= new TblShippTo();

            if (Session[Global.SESSION_SHIPTO] == null)
                shipto = shiptoDAO.GetAll(account.AccountID);
            else
                shipto = (TblShippTo)Session[Global.SESSION_SHIPTO];
            if (shipto != null)
            {
                txtName.Text = shipto.Name;
                txtAddr.Text = shipto.Address;
                //cb_city.Text= shipto.City;
                txtCountry.Text = shipto.Country;
                txtPhone.Text = shipto.Phone;
                txtEmail.Text = shipto.Email;
                txtContactName.Text = shipto.ContactName;
                //txtPostCode.Text = shipto.PostCode;

                // Dropdowlist lay value bang cach dua vao 1 list --> .value
                //ListItem lstState = cb_state.Items.FindByText(shipto.State);
                //cb_state.Text = lstState.Value;
                // Dropdowlist lay city
                //cb_city.Items.Insert(0, new ListItem(shipto.City, "0"));
                //cb_city.SelectedIndex = -1;
                cb_city.Items.Insert(0, new ListItem(shipto.City, " "));
                cb_city.SelectedIndex = -1;
                

            }
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE, true);
        }
    }
    protected void btnNext_Click(object sender, ImageClickEventArgs e)
    {
        if (!Page.IsValid) return;
        TblShippTo shipto;
        TblUserAccount account = new TblUserAccount();

        if (Session[Global.SESSION_SHIPTO] == null)
            shipto = new TblShippTo();
        else
            shipto = (TblShippTo)Session[Global.SESSION_SHIPTO];

        if (Session[Global.SESSION_LOGGED_USER] != null)
            account = (TblUserAccount)Session[Global.SESSION_LOGGED_USER];
        shipto.AccountID = account.AccountID;
        //shipto.Account = account;
        shipto.Name = txtName.Text.Trim();
        shipto.Address = txtAddr.Text.Trim();
        //shipto.City = txtCity.Text.Trim();
        //shipto.State = txtState.Text.Trim();
        shipto.City = cb_city.SelectedItem.Text;
        //shipto.State = cb_state.SelectedItem.Text;
        //shipto.PostCode = txtPostCode.Text.Trim();
        shipto.Country = "VietNam";
        shipto.Phone = txtPhone.Text.Trim();
        Session[Global.SESSION_SHIPTO] = shipto;

        List<TblOrderDetail> detail_lst = (List<TblOrderDetail>)Session[Global.SESSION_CART];
        foreach (TblOrderDetail detail in detail_lst)
        {
            //detail.TblOrder = new TblOrder();
            //detail.TblOrder.TblUserAccount = new TblUserAccount();
            //detail.TblOrder.TblUserAccount.ShipTo = shipto;
        }

        Session[Global.SESSION_CART] = detail_lst;

        string TOTAL_AMOUNT = ConfigurationManager.AppSettings["TotalAmount"].ToString();
        float dTotalAmount = float.Parse(TOTAL_AMOUNT);

        if (float.Parse(Session[Global.SESSION_TOTAL].ToString()) < dTotalAmount)
        {
            DeliveryFee();
        }
        else
        {
            //((TblShippTo)Session[Global.SESSION_SHIPTO]).DeliveryFee = 0;
        }
        Response.Redirect(string.Format("~/Client/OrderDetails.aspx"), true);
    }

    private void DeliveryFee()
    {
        throw new NotImplementedException();
    }
}
